import 'dart:async';

import 'package:flutter/material.dart';

class StreamCreateDemo extends StatefulWidget {
  StreamCreateDemo({Key key}) : super(key: key);

  @override
  _StreamCreateDemoState createState() => _StreamCreateDemoState();
}

class _StreamCreateDemoState extends State<StreamCreateDemo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('StreamCreateDemo'),
        elevation: 0.0,
      ),
      body: StreamDemoHome(),
    );
  }
}

class StreamDemoHome extends StatefulWidget {
  StreamDemoHome({Key key}) : super(key: key);

  @override
  _StreamDemoHomeState createState() => _StreamDemoHomeState();
}

class _StreamDemoHomeState extends State<StreamDemoHome> {
  StreamSubscription _streamDemoSubsciption;
  StreamController<String> _streamDemo;
  StreamSink _sinkDemo;
  // String _data = '...';

  @override
  void dispose() {
    _streamDemo.close();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();

    print('Create a Stream');
    // Stream<String> _streamDemo = Stream.fromFuture(fetchData());
    _streamDemo = StreamController<String>();
    _sinkDemo = _streamDemo.sink;

    print('Start listen on a Stream');
    _streamDemoSubsciption = _streamDemo.stream.listen(onData, onError:onError, onDone: onDone);

    print('initial compled');
  }

  void onData(String data) {
    // setState(() {
    //   _data = data;
    // });
    print('$data');
  }

  void onError(error) {
    print('$error');
  }

  void onDone() {
    print('done');
  }

  void _addDataToStream() async {
    print('Add data to stream.');

    String data = await fetchData();
    // _streamDemo.add(data);
    _sinkDemo.add(data);
  }

  void _pauseStream() {
    print('Pause subscription');
    _streamDemoSubsciption.pause();
  }

  void _resumStream() {
    print('resum subscription');
    _streamDemoSubsciption.resume();
  }

  void _cancelStream() {
    print('Cancel subscription');
    _streamDemoSubsciption.cancel();
  }

  Future<String> fetchData() async {
    await Future.delayed(Duration(seconds:3));
    // throw 'Somthing happened';
    return 'hello';
  }

  @override
  Widget build(BuildContext context) {
    return Container(
       child: Column(
         mainAxisAlignment: MainAxisAlignment.center,
         children: <Widget>[
           StreamBuilder(
             stream: _streamDemo.stream,
             initialData: '...',
             builder: (context, snapShot){
               return Text('${snapShot.data}');
             }
           ),
           Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              FlatButton(
                child: Text('Add'),
                onPressed: _addDataToStream,
              ),
              FlatButton(
                child: Text('Pause'),
                onPressed: _pauseStream,
              ),
              FlatButton(
                child: Text('Resum'),
                onPressed: _resumStream,
              ),
              FlatButton(
                child: Text('Cancel'),
                onPressed: _cancelStream,
              ),
            ]
          )
        ] 
       ),
    );
  }
}